package org.NooLab.itexx.co.targets;

import java.io.IOException;

import org.NooLab.itexx.ITexx;
import org.NooLab.itexx.ITexxFactory;
import org.NooLab.itexx.app.up.TxStartup;
import org.NooLab.itexx.co.CoWorker;
import org.NooLab.itexx.co.CoWorkerIntf;
import org.NooLab.itexx.co.CoWorkers;
import org.NooLab.itexx.co.ITexxModuleAppIntf;
import org.NooLab.utilities.net.GUID;
 


/**
 * 
 * 
 * the "Co-Classes" for configuration also create the initial startup-seed-info, 
 * if this does not exist yet.
 * 
 * 
 *
 */
public class CoTexx implements ITexxModuleAppIntf{

	ITexxFactory itxFactory ;
	
	CoWorker processDef ;
	CoWorkers coWorkers;
	ITexx itexx ;
/*
 
-prj: docoserv  
-homedir: D:/data/iTexx/DocumentServer/DocumentServer 
-cfg: docoserv-config.xml  
-notify:8988,8989
	 
*/
	
	public CoTexx( ITexxFactory factory, ITexx itx ){
		
		itxFactory = factory ;
		
		itexx = itx ;
		coWorkers = itexx.getCoWorkersContainer();
	}
	
	@Override
	public void configure(int tikaServerPort, int maxNumberOfConcurrents) throws IOException{
		
		String path ;
		
		processDef = coWorkers.getCoWorker(itexx) ;
		
		processDef.setJarFilename("NooLabTexx.jar") ;
		processDef.setMainClass("org.NooLab.texx.TexxApp") ;
		
		int ccn = coWorkers.GetConcurrentCountOf( processDef );
		if (ccn>maxNumberOfConcurrents){
			// in particular, we will not add this process definition to 
			// the items list in CoWorkers, and also we will not create an
			// instance of JavaProcessBuilder (jvm)
			return;
		}

		
		processDef.setJarFilePath("/bin"); // relative to system root   
		
		                                path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(),"/bin/lib"); 
		processDef.setExtLibrariesPath( path ) ;
										path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(),"/bin/plugins/texx/transforms");
		processDef.addExtLibrariesPath("path") ;


		
										path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(),"app/Texx");
		processDef.setAppWorkingDir( path) ; // relative to system root 
		
		processDef.addArgument( "-prj:itx");  
		
		// TODO make this dynamic
								path = TxStartup.getProjectBasePath();
								path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(), "");// app/Texx
		processDef.addArgument( "-homedir: "+path); 
		processDef.addArgument( "-cfg: texx-config.xml");
		processDef.addArgument( "-sourcemode: catalog");
		processDef.addArgument( "-context:itexx"); 
		
        						String instanceGuid = GUID.randomvalue() ;
		processDef.setInstanceGuid(instanceGuid);
		processDef.addArgument( "-waitfor:1");
		processDef.addArgument( "-appsecret:"+instanceGuid); 
		processDef.addArgument( "-name:_texx_"); // we use a small decoration

        				// we need a central catalog for all ports... from which they are issued
        				// such we build a map 
		   				int port = coWorkers.getNextPort( instanceGuid );
		   				String portStr = "in-"+port+",out-"+CoWorkerIntf._mainServerPort;
		   				processDef.setProposedPort(port); // this we need to send a release during startup
        processDef.addArgument( "-comm:"+ portStr);
        
        				 
        				String tikaportStr = "in-"+(tikaServerPort+1)+",out-"+tikaServerPort ; 
        				processDef.setTikaServerPort(tikaServerPort); // 
        processDef.addArgument( "-tika:"+ tikaportStr);
        			 	// Texx expects an argument like this...  -tika: in-7202,out-7201 , 
        			 	// where out is the server port of tikaserver
        
		processDef.addArgument( "-loglevel:2");
		
		/*
			-prj:itx
			-homedir:"D:/data/iTexx/app/Texx/" 
			-cfg:texx-config.xml
			-sourcemode:catalog 
			-context:itexx
			-loglevel:1
		 */
		
		processDef.setName(this.getClass().getSimpleName());
		
		processDef.create() ;

		coWorkers.add(processDef) ;
	}
	
	@Override
	public CoWorker getProcessDef() {
		return processDef;
	}

	@Override
	public void launch() throws Exception{
		processDef.launch() ;
	}
	
}
